package com.ruoyi.carUser.controller;

import com.ruoyi.carUser.service.CarService;
import com.ruoyi.carUser.util.JwtUtil;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.api.domain.CarInfo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class CarController {
    @Autowired
    private CarService carService;

    @RequestMapping("getCarList")
    public R getCarList(){
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes)attributes;
        HttpServletRequest request = sra.getRequest();
        //2 从请求头获取token
        String token = request.getHeader("token");
        Claims claims = null;
        try {
            claims = JwtUtil.parseJWT(token);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        Integer userId =  Integer.parseInt(claims.getSubject());
        List<CarInfo> carInfos = carService.carList(userId);
        return R.ok(carInfos);
    }


}
